﻿using System;
using System.Collections.Generic;
using FShare.DataModel;
using System.Net.Mail;
using System.Text;
using FShare.Helpers;

namespace FShare.Classes
{
    public sealed class MailService
    {
        private MailService() { }

        public static void SendLinks(List<FShareFile> files, string email)
        {
            if (files.Count == 0) 
            {
                return;
            }

            var body = new StringBuilder();

            body.Append("Hello!");
            body.Append(Environment.NewLine);
            body.Append(Environment.NewLine);
            body.Append("Your friend just uploaded some files to the FShare - File Sharing service and he wants to have this download and delete links:");

            files.ForEach(f =>
            {
                body.Append(Environment.NewLine);
                body.Append(Environment.NewLine);
                body.Append("File Name: ").Append(f.Name).Append(Environment.NewLine);
                body.Append("\tDownload link: ").Append(Utils.GenerateServerUrl("/Files/Download" + f.DownloadSecret));
                body.Append(Environment.NewLine);
                body.Append("\tDelete link: ").Append(Utils.GenerateServerUrl("/Files/Delete/" + f.DeleteSecret));
                body.Append(Environment.NewLine);
                body.Append("---------------------------------------");
            });

            body.Append(Environment.NewLine);
            body.Append(Environment.NewLine);
            body.Append(Environment.NewLine);
            body.Append("Greetings,").AppendLine("FShare - File sharing service.");
            body.Append(Environment.NewLine);

            var message = new MailMessage();
            message.Subject = "Download Links";
            message.From = new MailAddress("fshareservice@gmail.com");
            message.To.Add(email);
            message.IsBodyHtml = false;
            message.Body = body.ToString();

            var client = new SmtpClient();
            client.SendAsync(message, null);
        }
    }
}